package com.evon.yardmanagement.ui.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.SessionAware;

import com.evon.yardmanagement.export.ExportDataManager;
import com.evon.yardmanagement.export.ParkingSlotExportableData;
import com.evon.yardmanagement.model.ParkingSlot;
import com.evon.yardmanagement.service.ParkingSlotsService;

public class ExportParkingSlotAction implements ParameterAware, SessionAware {

	private Map parameters;
	private Map<String, Object> session;
	private int sectionId;

	private ParkingSlotsService service = new ParkingSlotsService();
	private List<ParkingSlot> parkingSlots = Collections.EMPTY_LIST;
	
	
	
	// availableYard = YardTransformer.transform(yards.get(0));
	private InputStream filePsStream;
	

	public int getSectionId() {
		return sectionId;
	}

	public void setSectionId(int sectionId) {
		this.sectionId = sectionId;
	}

	public void setFilePsStream(InputStream arg) {
		filePsStream = arg;
	}

	public InputStream getFilePsStream() {
		filePsStream = new ByteArrayInputStream(exportSectionToExcelFile()
				.toByteArray());
		return filePsStream;
	}

	public String execute() {
		return "success";
	}

	private ByteArrayOutputStream exportSectionToExcelFile() {
		parkingSlots = getSectionId()!=0? service.fetchParkingSlotBySectionId(getSectionId())
				:Collections.EMPTY_LIST;
		ParkingSlotExportableData exportableData = new ParkingSlotExportableData(
				parkingSlots);
		ExportDataManager<ParkingSlotExportableData> exportManager = new ExportDataManager<ParkingSlotExportableData>(
				exportableData);
		return exportManager.exportToExcel();
	}

	public Map getParameters() {
		return parameters;
	}

	public void setParameters(Map param) {
		System.out.println("set parameter");
		this.parameters = param;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;

	}

	public List<ParkingSlot> getSections() {
		return parkingSlots;
	}

	public void setSections(List<ParkingSlot> sections) {
		this.parkingSlots = sections;
	}

}
